import java.util.concurrent.ForkJoinTask;
import java.util.concurrent.TimeUnit;

public class NumberTask extends ChainTask {
    public NumberTask(int taskNumber) {
        super(taskNumber);
    }

    @Override
    protected ForkJoinTask<Void> forkTask() {
        return new NumberTask(getTaskNumber() + 1).fork();
    }

    @Override
    protected void nextChain() {
        new WriterTask(1).fork().join();
    }

    @Override
    protected void doAction(int firstNumber, int lastNumber) throws InterruptedException {
        StringBuilder builder = new StringBuilder();
        for (int i = firstNumber; i <= lastNumber; i++) {
            String number = Loader.padNumber(i, 3);

            for (char firstLetter : Loader.getLetters()) {
                for (char secondLetter : Loader.getLetters()) {
                    for (char thirdLetter : Loader.getLetters()) {
                        builder.append(firstLetter);
                        builder.append(number);
                        builder.append(secondLetter);
                        builder.append(thirdLetter);
                        builder.append(Loader.getRegionCode());
                        builder.append(System.lineSeparator());
                    }
                }
            }

            if (!Loader.getQueue(getTaskNumber()).offer(
                    builder.toString(),
                    5, TimeUnit.SECONDS
            )) {
                // Most likely file error
                break;
            }

            builder.setLength(0);
        }
    }
}
